package com.mi.cmdtest;

import java.util.Arrays;

/**
 * Created by liuSheng on 2016/11/5.
 */

public class ArrayUtils {

    /**
     * 拼接 数组
     *
     * @param first
     * @param rest
     * @return
     */
    public static byte[] concatAll(byte[] first, byte[]... rest) {
        int totalLength = first.length;
        for (byte[] array : rest) {
            totalLength += array.length;
        }
        byte[] result = Arrays.copyOf(first, totalLength);
        int offset = first.length;
        for (byte[] array : rest) {
            System.arraycopy(array, 0, result, offset, array.length);
            offset += array.length;
        }
        return result;
    }

    /**
     * 蓝牙模块返回的指令，转变成带分隔符的字符串，如AA-EE-08-40-83-89-9E-08-71-6E-33-AA
     *
     * @param value
     * @return
     */
    public static String backCMD2String(byte[] value) {
        if (value == null) return "";
        StringBuffer sb = new StringBuffer();
        for (byte by : value) {
            int i = by & 0xff;
            String hexString = Integer.toHexString(i);
            if (hexString.length() < 2) {
                sb.append("0");
            }
            sb.append(hexString.toUpperCase());
            sb.append("-");
        }
        sb.deleteCharAt(sb.length() - 1);////移除最后一个空格
        return sb.toString();
    }
    /**
     * 数据位 9位
     *
     * @param data
     * @return
     */
    public static String getFirmVer(byte[] data) {
        StringBuilder builder = new StringBuilder();
        for (byte b :
                data) {
            if (b != 0)
                builder.append((char) b);
        }
        return builder.toString();
    }

    /**
     * 蓝牙模块返回的序列号字符串，如AAEE084083899E08716E33AA
     *
     * @param value
     * @return
     */
    public static String getSerialString(byte[] value) {
        if (value == null) return "";
        StringBuffer sb = new StringBuffer();
        for (byte by : value) {
            int i = by & 0xff;
            String hexString = Integer.toHexString(i);
            if (hexString.length() < 2) {
                sb.append("0");
            }
            sb.append(hexString.toUpperCase());
        }
        return sb.toString();
    }

    /**
     * 从原数据中截取指定部分
     *
     * @param data  原数据
     * @param start 起始点
     * @param end   终点
     */
    public static byte[] getPartData(byte[] data, int start, int end) {
        byte[] result = new byte[end - start + 1];
        System.arraycopy(data, start, result, 0, end - start + 1);
        return result;
    }

    /**
     * 两个byte转成int 高位在前 低位在后
     *
     * @param h 高位
     * @param l 低位
     * @return int
     */
    public static int bytes2Int(byte h, byte l) {
        return ((h << 8) & 0xff00) | (l & 0xff);
    }

    /**
     * 10进制是多少，16进制就发多少；如15-->0x15（而非0x0f）
     *
     * @param a
     * @return
     */
    public static int ten2Sixteen(int a) {
        return (a / 10) * 16 + a % 10;
    }

    /**
     * 将10进制的数转换成字节数组，数组的第一个元素就是目标（能被蓝牙模块识别的那种哦！！！）
     *
     * @param num
     * @return 字节数组
     */
    public static byte[] int2bytes(int num) {
        byte[] src = new byte[4];
        src[3] = (byte) ((num >> 24) & 0xFF);
        src[2] = (byte) ((num >> 16) & 0xFF);
        src[1] = (byte) ((num >> 8) & 0xFF);
        src[0] = (byte) (num & 0xFF);
        return src;
    }

    /**
     * 每 1 个字节表示一位数的 拼接
     *
     * @param bytes 单 字节 数组
     * @return 1-2-3
     */
    public static String getDataByBytes(byte[] bytes) {
        if (bytes == null) {
            return null;
        }
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < bytes.length; i++) {
            sb.append(bytes[i] & 0xff);
            sb.append("-");
        }
        sb.deleteCharAt(sb.length() - 1);
        return sb.toString();
    }

    /**
     * 温度 由 2 字节组成
     *
     * @param bytes 双字节 数组
     * @return 25.1-25.2-25.3
     */
    public static String getDataByBytes2(byte[] bytes) {
        if (bytes == null || bytes.length == 1) {
            return null;
        }
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i + 1 < bytes.length; ) {
            float f = (float) (bytes2Int(bytes[i], bytes[i + 1]) * 0.02f - 273.15);
            sb.append(f);
            sb.append("-");
            i += 2;
        }
        if (sb.length() > 1) {
            sb.deleteCharAt(sb.length() - 1);
        }
        return sb.toString();
    }

    /**
     * 根据4个字节获取时间
     *
     * @param data 4字节数组
     * @return int 时间戳
     */
    public static int getTimeByData(byte[] data) {//5,6,7,8

        int value = 0;
        if (data == null) {
            return value;
        }
        if (data.length == 4) {
            value = ((data[0] & 0xff) << 24)
                    | ((data[1] & 0xff) << 16)
                    | ((data[2] & 0xff) << 8)
                    | ((data[3] & 0xff));
        }
        return value;
    }

    public static byte[] getKeyByKeyString(String keyStr) {
        String[] split = keyStr.split("-");
        byte[] key = new byte[split.length];
        for (int i = 0; i < key.length; i++) {
            key[i] = (byte) ((0xff) & Integer.parseInt(split[i], 16));
        }
        return key;
    }

    /**
     * @param b1 高位
     * @param b2 低位
     * @return
     */
    public static short bytesToShort(byte b1, byte b2) {
        short value;
        value = (short) (((b1 << 8) & 0xff00)
                | ((b2) & 0xff));

        return value;
    }

    public static char[] int2chars(int num) {
        char[] src = new char[2];
        src[1] = (char) ((num >> 16) & 0xFF);
        src[0] = (char) (num & 0xFF);
        return src;


    }
    /**
     * 蓝牙模块广播字符串
     *
     * @param value
     * @return
     */
    public static String getBleName(byte[] value) {
        if (value == null) return "";
        StringBuffer sb = new StringBuffer();
        for (byte by : value) {
            int i = by & 0xff;
            String hexString = Integer.toHexString(i);
            if (hexString.length() < 2) {
                sb.append("0");
            }
            sb.append(hexString);
        }
        return sb.toString();
    }
    public static String getBleNameByScan(byte[] scan) {
        byte[] data_name = new byte[8];
        for (int i = 0; i < data_name.length; i++) {
            data_name[i] = scan[9 + i];
        }
        return ArrayUtils.getBleName(data_name);
    }
}
